﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKElementFinish.WallInsulationLayer.Controllers;

namespace YJKElementFinish.WallInsulationLayer.Forms
{
	public partial class AddInsulationLayerForm : System.Windows.Forms.Form
	{
		public double Thick
		{
			get
			{
				return Convert.ToDouble(this.cmbThick.Text);
			}
		}

		public string SelectLevelValue
		{
			get
			{
				return this.cmbTopLevel.Text;
			}
		}

		public WallType SelectWallType
		{
			get
			{
				return this.tvWallTree.SelectedNode.Tag as WallType;
			}
		}

		public string WallName
		{
			get
			{
				string result = this.tvWallTree.SelectedNode.Text;
				if (this.tvWallTree.SelectedNode.Tag == null)
				{
					result = "保温-" + this.Thick.ToString() + "-聚苯板";
				}
				return result;
			}
		}

		public bool CreateNewWallType
		{
			get
			{
				return this.m_bIsCreateNewWallType;
			}
		}

		public AddInsulationLayerForm.FormResultStyle ResultStyle
		{
			get
			{
				return this.m_FormResultStyle;
			}
		}

		public AddInsulationLayerForm(Autodesk.Revit.DB.Document doc, List<Level> lstLevels, List<WallType> lstWallTypes)
		{
			this.InitializeComponent();
			this.m_doc = doc;
			this.m_lstLevels = lstLevels;
			this.m_lstBasicWalls = lstWallTypes;
		}

		private void AddInsulationLayerForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.InitTopLevel();
			this.InitWallTree();
			this.ReadThickData();
			this.ReadFormData();
			this.SetControlStatus();
		}

		private void AddInsulationLayerForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.ResultStyle == AddInsulationLayerForm.FormResultStyle.FRS_Draw && ((System.Windows.Forms.Form)sender).DialogResult == DialogResult.Cancel)
			{
				this.m_FormResultStyle = AddInsulationLayerForm.FormResultStyle.FRS_OK;
				e.Cancel = true;
			}
			if (((System.Windows.Forms.Form)sender).DialogResult != DialogResult.OK)
			{
				return;
			}
			if (this.m_FormResultStyle != AddInsulationLayerForm.FormResultStyle.FRS_OK)
			{
				return;
			}
			if (!this.VerifyControlValid())
			{
				e.Cancel = true;
				return;
			}
			this.SaveFormData();
			if (this.ModifyThickFile())
			{
				this.SaveThickData();
			}
			if (-1 == this.cmbTopLevel.SelectedIndex)
			{
				this.m_strLevelValue = this.cmbTopLevel.Text;
			}
			else
			{
				this.m_strLevelValue = "";
			}
			this.m_FormResultStyle = AddInsulationLayerForm.FormResultStyle.FRS_Draw;
		}

		private void InitializeComboBox(System.Windows.Forms.ComboBox control, List<string> lstTypeName)
		{
			control.Items.Clear();
			foreach (string item in lstTypeName)
			{
				control.Items.Add(item);
			}
			if (control.Items.Count > 0)
			{
				control.SelectedIndex = 0;
			}
		}

		private void SetComboBoxSelectedIndex(System.Windows.Forms.ComboBox control, int nIndex)
		{
			if (nIndex < 0 || nIndex >= control.Items.Count)
			{
				return;
			}
			control.SelectedIndex = nIndex;
		}

		private void InitTopLevel()
		{
			List<string> list = new List<string>();
			for (int i = 0; i < this.m_lstLevels.Count; i++)
			{
				Level level = this.m_lstLevels.ElementAt(i);
				list.Add(level.Name);
			}
			this.InitializeComboBox(this.cmbTopLevel, list);
			if ("" == this.m_strLevelValue)
			{
				this.SetComboBoxSelectedIndex(this.cmbTopLevel, 0);
				return;
			}
			this.cmbTopLevel.SelectedIndex = -1;
			this.cmbTopLevel.Text = this.m_strLevelValue;
		}

		private void InitWallTree()
		{
			this.tvWallTree.Nodes.Clear();
			this.InitTree("预设", null);
			this.InitTree("普通墙", this.m_lstBasicWalls);
			this.tvWallTree.ExpandAll();
		}

		private void InitTree(string strParentName, List<WallType> lstWallTypes)
		{
			if (lstWallTypes == null)
			{
				this.InitPresetTree(strParentName);
				return;
			}
			TreeNode treeNode = new TreeNode();
			treeNode.Text = strParentName;
			foreach (WallType wallType in lstWallTypes)
			{
				TreeNode treeNode2 = new TreeNode();
				treeNode2 = new TreeNode();
				treeNode2.Text = wallType.Name;
				treeNode2.Tag = wallType;
				treeNode.Nodes.Add(treeNode2);
			}
			this.tvWallTree.Nodes.Add(treeNode);
		}

		private void InitPresetTree(string strParentName)
		{
			TreeNode treeNode = new TreeNode();
			treeNode.Text = strParentName;
			TreeNode treeNode2 = new TreeNode();
			treeNode2.Text = "聚苯板";
			treeNode.Nodes.Add(treeNode2);
			this.tvWallTree.Nodes.Add(treeNode);
		}

		private void SetControlStatus()
		{
			if (this.tvWallTree.SelectedNode.Parent == null)
			{
				if ("预设" == this.tvWallTree.SelectedNode.Text)
				{
					this.cmbThick.Enabled = true;
					this.m_bIsCreateNewWallType = true;
					this.tvWallTree.SelectedNode = this.tvWallTree.Nodes[0].FirstNode;
					return;
				}
				this.cmbThick.Enabled = false;
				this.m_bIsCreateNewWallType = false;
				this.tvWallTree.SelectedNode = this.tvWallTree.Nodes[1].FirstNode;
				return;
			}
			else
			{
				if ("预设" == this.tvWallTree.SelectedNode.Parent.Text)
				{
					this.cmbThick.Enabled = true;
					this.m_bIsCreateNewWallType = true;
					return;
				}
				this.cmbThick.Enabled = false;
				this.m_bIsCreateNewWallType = false;
				return;
			}
		}

		private void ReadFormData()
		{
			string path = Product.UserDataLocation + "AddInsulationLayerForm.dat";
			if (!File.Exists(path))
			{
				this.tvWallTree.SelectedNode = this.tvWallTree.Nodes[0].Nodes[0];
				this.cmbThick.Text = "70";
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			string a = Convert.ToString(streamReader.ReadLine());
			string a2 = Convert.ToString(streamReader.ReadLine());
			foreach (object obj in this.tvWallTree.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (!(a != treeNode.Text))
				{
					foreach (object obj2 in treeNode.Nodes)
					{
						TreeNode treeNode2 = (TreeNode)obj2;
						if (a2 == treeNode2.Text)
						{
							this.tvWallTree.SelectedNode = treeNode2;
							break;
						}
					}
				}
			}
			if (this.tvWallTree.SelectedNode == null)
			{
				this.tvWallTree.SelectedNode = this.tvWallTree.Nodes[0].Nodes[0];
			}
			this.cmbThick.Text = Convert.ToString(streamReader.ReadLine());
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveFormData()
		{
			FileStream fileStream = new FileStream(Product.UserDataLocation + "AddInsulationLayerForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.tvWallTree.SelectedNode.Parent.Text);
			streamWriter.WriteLine(this.tvWallTree.SelectedNode.Text);
			streamWriter.WriteLine(this.cmbThick.Text);
			streamWriter.WriteLine(base.Location.X.ToString());
			streamWriter.WriteLine(base.Location.Y.ToString());
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool LoadFile()
		{
			try
			{
				this.m_xmlDoc = new XmlDocument();
				this.m_xmlDoc.Load(this.m_strFullFile);
			}
			catch (Exception)
			{
				YJKMessageBox.Show("找不到保温层厚度的配置文件!", MessageBoxButtons.OK);
				return false;
			}
			return true;
		}

		private void ReadThickData()
		{
			if (!this.LoadFile())
			{
				return;
			}
			XmlElement documentElement = this.m_xmlDoc.DocumentElement;
			if (documentElement == null)
			{
				return;
			}
			this.cmbThick.Items.Clear();
			foreach (object obj in documentElement.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode.Name == "Thick")
				{
					string attribute = (xmlNode as XmlElement).GetAttribute("value");
					this.cmbThick.Items.Add(attribute);
				}
			}
		}

		private void SaveThickData()
		{
			XmlNode xmlNode = this.m_xmlDoc.SelectSingleNode("InsulationLayer");
			xmlNode.RemoveAll();
			List<string> list = new List<string>();
			for (int i = 0; i < 9; i++)
			{
				if (i == 0)
				{
					list.Add(this.cmbThick.Text);
				}
				else
				{
					list.Add(this.cmbThick.Items[i - 1].ToString());
				}
			}
			list.Sort(new ThickComparer());
			foreach (string value in list)
			{
				XmlElement xmlElement = this.m_xmlDoc.CreateElement("Thick");
				xmlElement.SetAttribute("value", value);
				xmlNode.AppendChild(xmlElement);
			}
			this.m_xmlDoc.Save(this.m_strFullFile);
		}

		private bool VerifyControlValid()
		{
			string text = "";
			string text2 = this.cmbTopLevel.Text;
			if ("" == text2)
			{
				text = "高度参数错误。";
			}
			if ("" == this.cmbThick.Text)
			{
				text += "\n墙厚参数应为正整数,请检查墙厚。";
			}
			if ("" != text)
			{
				YJKMessageBox.Show(text, MessageBoxButtons.OK);
				return false;
			}
			return true;
		}

		private bool ModifyThickFile()
		{
			return !this.cmbThick.Items.Contains(this.cmbThick.Text);
		}

		private void cmbTopLevel_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b')
			{
				e.Handled = true;
				return;
			}
			if (e.KeyChar == '\b')
			{
				this.cmbTopLevel.Text = "";
			}
		}

		private void cmbThick_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b')
			{
				e.Handled = true;
				return;
			}
		}

		private void AddInsulationLayerForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (Keys.Escape == e.KeyCode)
			{
				this.m_FormResultStyle = AddInsulationLayerForm.FormResultStyle.FRS_Exit;
				base.Close();
			}
		}

		private void cmbTopLevel_TextChanged(object sender, EventArgs e)
		{
			if (this.cmbTopLevel.Text == "0")
			{
				this.cmbTopLevel.Text = "";
				return;
			}
		}

		private void cmbThick_TextChanged(object sender, EventArgs e)
		{
			if (this.cmbThick.Text == "0")
			{
				this.cmbThick.Text = "";
				return;
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.m_FormResultStyle = AddInsulationLayerForm.FormResultStyle.FRS_OK;
		}

		private void tvWallTree_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.SetControlStatus();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			PositioningCurveController.CreateWall = true;
			this.m_FormResultStyle = AddInsulationLayerForm.FormResultStyle.FRS_Exit;
			base.DialogResult = DialogResult.Cancel;
		}

		private Document m_doc;

		private AddInsulationLayerForm.FormResultStyle m_FormResultStyle = AddInsulationLayerForm.FormResultStyle.FRS_OK;

		private List<Level> m_lstLevels = new List<Level>();

		private List<WallType> m_lstBasicWalls = new List<WallType>();

		private bool m_bIsCreateNewWallType;

		private string m_strFullFile = Product.DataLocation + "\\InsulationLayerThick.xml";

		private XmlDocument m_xmlDoc;

		private string m_strLevelValue = "";

		public enum FormResultStyle
		{
			FRS_OK = 1,
			FRS_Draw,
			FRS_Exit
		}
	}
}
